<?php
/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2019/2/25
 * Time: 21:36
 */

namespace app\common\service;


use app\common\model\Channel;

class ChannelService
{
    /**
     * 批量获取渠道
     * @param $ids
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function batchChannelByIds($ids)
    {
        $map[] = ['id', 'in', $ids];
        $model = new Channel();
        $list = $model->where($map)->select();
        $newList = [];
        foreach ($ids as $v){
            foreach($list as $value){
                if($value['id'] == $v){
                    $newList[$v] = $value;
                }
            }
        }
        return $newList;
    }

    /**
     * 通过code获取渠道
     * @param $code
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getChannelByCode($code)
    {
        if(!$code){
            return null;
        }
        $map = [
            'code'=>$code,
        ];
        $model = new Channel();
        return $model->where($map)->find();
    }

    /**
     * 获取所有的渠道列表
     * @author liud
     * @return array
     */
    public static function getChannelMapping()
    {
        $model = new Channel();
        return $model->where([])->column('id,name');
    }

    public static function getChannelByChannelId($channelId)
    {
        $model = new Channel();
        return $model->where(['id'=>$channelId])->find();
    }
}